package com.stox.charting.ui.swing.model.uiUnit;

import java.awt.Graphics;
import java.awt.Rectangle;

import com.stox.charting.ui.swing.model.ChartModel;
import com.stox.charting.ui.swing.model.price.PriceChartModel;
import com.stox.core.model.IBar;

public class LinePriceUiUnit<T extends IBar> extends PriceBarUiUnit<T> {

	private int x1;
	private int x2;
	private int y1;
	private int y2;
	
	public LinePriceUiUnit(PriceChartModel<T> model, int index) {
		super(model, index);
	}

	@Override
	public void processUi(double width, double factor) {
		ChartModel<T> model = getModel();
		Rectangle r = model.getChart().getVisibleRect();
		setWidth(width);
		x1 = (int)(width*(model.getViewport().getStartIndex() - getIndex()-1));
		setX(x1);
		x2 = (int)(x1+width);
		IBar bar = getValue();
		double min = model.getViewport().getMinimumValueVisible();
		double height = r.getHeight();
		y1 = (int)(height - (bar.getPreviousClose()-min)*factor);
		y2 = (int)(height - (bar.getClose()-min)*factor);
	}
	
	@Override
	public void paint(Graphics g) {
		g.setColor(getColor());
		g.drawLine(x1,y1,x2, y2);
	}

}
